package com.ql.gamelogm.controller;

import com.ql.gamelogm.entities.Log;
import com.ql.gamelogm.entities.Mail;
import com.ql.gamelogm.entities.RedisCache;
import com.ql.gamelogm.service.LogService;
import com.ql.gamelogm.service.MailService;
import com.ql.gamelogm.service.impl.MailServiceImpl;
import com.ql.gamelogm.utils.IpUnit;
import com.ql.gamelogm.utils.MailUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
public class MailController {
    /*导入邮件服务*/
    @Autowired
    private MailServiceImpl mailServiceImpl;
    @Autowired
    private LogService logService;
    @Autowired
    private MailUtils mailUtils;
    @Autowired
    private MailService mailService;
    @Autowired
    private RedisCache redisCache;

    @PostMapping(value = "/sendMail")
    public String SendMailTo(HttpServletRequest httpServletRequest, HttpSession httpSession){
        try{
            String mailto=httpServletRequest.getParameter("email_to");
            String email_subject=httpServletRequest.getParameter("email_subject");
            String message=httpServletRequest.getParameter("message");
            boolean isSendMailOky=mailService.SendMail(mailto,email_subject , message);
            Date date=new Date();
            date.setTime(System.currentTimeMillis());
            if(isSendMailOky){
                Mail mail=new Mail();
                mail.setState("已读");
                mail.setSendTo(mailto);
                mail.setSubject(email_subject);
                mail.setContent(message);
                mail.setSendFrom("2985761683@qq.com");
                mail.setDate(date);
                mailService.SaveMailSended(mail);
                //存发送邮件操作记录
                String id=(String)httpSession.getAttribute("id");
                String ipaddr= IpUnit.getIpAddr();
                Log log=new Log();
                log.setDate(date);
                log.setOperator(id);
                log.setOperation("发送邮件");
                log.setIpaddr(ipaddr);
                logService.savelog(log);
            }
            httpSession.setAttribute("isSendMailOky",isSendMailOky);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "redirect:/findAllMailsInBox";
    }

    //查找所有收件箱中邮件
    @GetMapping(value = "/findAllMailsInBox")
    public String MailInBox(HttpSession httpSession,Model model,@RequestParam(value = "pageNum",defaultValue = "0")int PageNum,@RequestParam(value = "pageSize",defaultValue = "8")int
            PageSize) throws Exception {
        Page<Mail> mailInBoxList=mailServiceImpl.getMailsInBoxList(PageNum,PageSize);
        model.addAttribute("mailInBoxList",mailInBoxList);
        //查操作记录
        String id1=(String)httpSession.getAttribute("id");
        Date date=new Date();
        date.setTime(System.currentTimeMillis());
        String ipaddr= IpUnit.getIpAddr();
        Log log=new Log();
        log.setDate(date);
        log.setOperator(id1);
        log.setOperation("收邮箱查看");
        log.setIpaddr(ipaddr);
        logService.savelog(log);
        return "emailinbox";
    }
    //查找所有已发送邮件
    @GetMapping(value = "/findAllMailsSended")
    public String MailSended(HttpSession httpSession,Model model,@RequestParam(value = "pageNum",defaultValue = "0")int PageNum,@RequestParam(value = "pageSize",defaultValue = "8")int
            PageSize){
        Page<Mail> mailSendedList=mailServiceImpl.getMailsSendedList(PageNum,PageSize);
        model.addAttribute("mailSendedList",mailSendedList);
        //查操作记录
        String id2=(String)httpSession.getAttribute("id");
        Date date=new Date();
        date.setTime(System.currentTimeMillis());
        String ipaddr= IpUnit.getIpAddr();
        Log log=new Log();
        log.setDate(date);
        log.setOperator(id2);
        log.setOperation("已发送邮件查看");
        log.setIpaddr(ipaddr);
        logService.savelog(log);
        return "emailsended";
    }

    //查找所有已删除邮件
    @GetMapping(value = "/findAllMailsDeleted")
    public String MailDeleted(HttpSession httpSession,Model model,@RequestParam(value = "pageNum",defaultValue = "0")int PageNum,@RequestParam(value = "pageSize",defaultValue = "8")int
            PageSize){
        Page<Mail> mailDeletedList=mailServiceImpl.getMailsDeletedList(PageNum,PageSize);
        model.addAttribute("mailDeletedList",mailDeletedList);
        //查操作记录
        String id3=(String)httpSession.getAttribute("id");
        Date date=new Date();
        date.setTime(System.currentTimeMillis());
        String ipaddr= IpUnit.getIpAddr();
        Log log=new Log();
        log.setDate(date);
        log.setOperator(id3);
        log.setOperation("已删除邮箱查看");
        log.setIpaddr(ipaddr);
        logService.savelog(log);
        return "emaildeleted";
    }
    //收件箱邮件内容
    @RequestMapping("/findMailInBoxById")
    public String findMailInBoxById(int id,Model model,HttpSession httpSession) {
        String id4=(String)httpSession.getAttribute("id");
        redisCache.updateMailState(id);
        List<Mail> mailInBoxById = mailService.findMailInBoxById(id);
        int c1=mailInBoxById.size();
        if(c1!=0){
            httpSession.setAttribute("c1", c1);
            model.addAttribute("mailInBoxById",mailInBoxById);
            Date date=new Date();
            date.setTime(System.currentTimeMillis());
            String ipaddr= IpUnit.getIpAddr();
            Log log=new Log();
            log.setDate(date);
            log.setOperator(id4);
            log.setOperation("收件箱邮件查看");
            log.setIpaddr(ipaddr);
            logService.savelog(log);
            mailService.updateMailState(id);

        }
        return "emailcontent";

    }

    //已发送邮件内容
    @RequestMapping("/findMailSendedById")
    public String findMailSendedBySendTo( int id,Model model,HttpSession httpSession) {
        List<Mail> mailSendedById = mailService.findMailSendedById(id);
        int c2=mailSendedById.size();
        httpSession.setAttribute("c2", c2);
        model.addAttribute("mailSendedById",mailSendedById);
        String id5=(String)httpSession.getAttribute("id");
        Date date=new Date();
        date.setTime(System.currentTimeMillis());
        String ipaddr= IpUnit.getIpAddr();
        Log log=new Log();
        log.setDate(date);
        log.setOperator(id5);
        log.setOperation("已发送邮件查看");
        log.setIpaddr(ipaddr);
        logService.savelog(log);
        return "emailcontent";
        /*return "emailsended";*/

    }

    //已删除邮件内容
    @RequestMapping("/findMailDeletedById")
    public String findMailDeletedBySendTo( int id,Model model,HttpSession httpSession) {

        List<Mail> mailDeletedById = mailService.findMailDeletedById(id);
        int c3=mailDeletedById.size();
        httpSession.setAttribute("c3", c3);
        model.addAttribute("mailDeletedById",mailDeletedById);
        String id6=(String)httpSession.getAttribute("id");
        Date date=new Date();
        date.setTime(System.currentTimeMillis());
        String ipaddr= IpUnit.getIpAddr();
        Log log=new Log();
        log.setDate(date);
        log.setOperator(id6);
        log.setOperation("已删除邮件查看");
        log.setIpaddr(ipaddr);
        logService.savelog(log);
        mailService.updateMailState(id);
        return "emailcontent";
        /*return "emaildeleted";*/
    }

    /*删除收件箱中邮件*/
    @GetMapping("/delMailInBoxById")
    public String delMailInBoxById(@RequestParam(value = "mailId")int id,Model model,HttpSession httpSession){
        /*删除数据库收件箱邮件*/
        int i = mailService.DeleteMailInBoxById(id);
        Mail mail = redisCache.deleteMailInBox(id);
        mail.setId(i+1);
        boolean isDeloky = mailService.SaveMailDeleted(mail);
        if(isDeloky){
            String id6=(String)httpSession.getAttribute("id");
            Date date=new Date();
            date.setTime(System.currentTimeMillis());
            String ipaddr= IpUnit.getIpAddr();
            Log log=new Log();
            log.setDate(date);
            log.setOperator(id6);
            log.setOperation("删除邮件");
            log.setIpaddr(ipaddr);
            logService.savelog(log);
        }
        httpSession.setAttribute("idDeloky",isDeloky);
        return "redirect:/findAllMailsInBox";
    }

    /*删除已发送邮件*/
    @GetMapping("/delMailSendedById")
    public String delMailSendedById(@RequestParam(value = "mailId")int id,Model model,HttpSession httpSession){
        /*删除数据库收件箱邮件*/
        int i = mailService.DeleteMailSendedById(id);
        Mail mail = redisCache.deleteMailSended(id);
        mail.setId(i+1);
        boolean isDeloky1 = mailService.SaveMailDeleted(mail);
        if(isDeloky1){
            String id6=(String)httpSession.getAttribute("id");
            Date date=new Date();
            date.setTime(System.currentTimeMillis());
            String ipaddr= IpUnit.getIpAddr();
            Log log=new Log();
            log.setDate(date);
            log.setOperator(id6);
            log.setOperation("删除邮件");
            log.setIpaddr(ipaddr);
            logService.savelog(log);
        }
        httpSession.setAttribute("isDeloky1",isDeloky1);
        return "redirect:/findAllMailsSended";
    }

}
